<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span>{{ selectTitle }}</span>
      <span
        class="iconfont title-icon"
        :style="comStyle"
        @click="showChoice = !showChoice"
        >&#xe6eb;</span
      >
      <div class="select-con" v-show="showChoice">
        <div
          class="select-item"
          v-for="item in selectType"
          :key="item.key"
          @click="handleSelect(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件,工具js,第三方插件js,json文件,图片文件等等）
//例如：import《组件名称》from'《组件路径》';
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      chartEntity: null,
      allData: null,
      showChoice: false,
      choiceType: "map",
      title_font_size: 0,
    };
  },
  //计算属性:类似于data概念,有缓存效果,用于不经常修改的数据
  computed: {
    // 选择类型
    selectType() {
      if (!this.allData) {
        return [];
      } else {
        // 过滤当前的类型
        return this.allData.type.filter((item) => item.key !== this.choiceType);
      }
    },
    // 根据当前类型显示标题
    selectTitle() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.choiceType].title;
      }
    },
    comStyle() {
      // console.log(this.title_font_size);
      return {
        fontSize: this.title_font_size + "px",
        color: getThemeValue(this.theme).titleColor,
      };
    },
    //监控data中的数据变化
    ...mapState(["theme"]),
  },
  // 监控data中的数据变化
  watch: {
    theme() {
      // 销毁当前图表
      this.chartEntity.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    },
  },
  //方法集合
  methods: {
    initChart() {
      this.chartEntity = this.$echarts.init(this.$refs.trend_ref, this.theme);
      let initOption = {
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          left: 20,
          top: "15%",
          icon: "circle",
        },
        grid: {
          top: "35%",
          bottom: "5%",
          left: "5%",
          right: "5%",
          containLabel: true,
        },
      };
      this.chartEntity.setOption(initOption);
    },
    getData(ret) {
      // let { data: ret } = await this.$http.get("trend");
      this.allData = ret;
      this.updateChart();
    },
    updateChart() {
      // 半透明的颜色值
      let colorArr1 = [
        "rgba(11,168,44,0.5)",
        "rgba(44,110,255,0.5)",
        "rgba(22,242,217,0.5)",
        "rgba(254,33,30,0.5)",
        "rgba(250,105,0,0.5)",
      ];
      // 全透明的颜色值
      let colorArr2 = [
        "rgba(11,168,44,0)",
        "rgba(44,110,255,0)",
        "rgba(22,242,217,0)",
        "rgba(254,33,30,0)",
        "rgba(250,105,0,0)",
      ];

      // 类目轴的数据
      let timeArr = this.allData.common.month;
      // y轴的数据
      let valueArr = this.allData[this.choiceType].data;
      let seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.choiceType,
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              },
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });
      // 图例
      let legendArr = valueArr.map((item) => item.name);

      let dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartEntity.setOption(dataOption);
    },
    screenAdapter() {
      this.title_font_size = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      let adapterOption = {
        legend: {
          itemWidth: this.title_font_size / 2,
          itemHeight: this.title_font_size / 2,
          itemGap: this.title_font_size,
          textStyle: {
            fontSize: this.title_font_size,
          },
        },
      };

      this.chartEntity.setOption(adapterOption);
      this.chartEntity.resize();
    },
    handleSelect(currType) {
      this.choiceType = currType;
      this.updateChart();
      this.showChoice = false;
    },
  },
  beforeCreate() {}, //生命周期-创建之前
  created() {
    // 注册回调函数
    this.$socket.registerCallBack("trend", this.getData);
  }, //生命周期-创建完成（可以访问当前this实例）
  beforeMount() {}, //生命周期-挂载之前
  mounted() {
    this.initChart();
    // this.getData();
    // 发送数据给服务器,
    this.$socket.send({
      action: "getData",
      socketType: "trend",
      chartName: "trend",
      value: "",
    });

    // 不要在函数中加(), 否则只会调用一次
    window.addEventListener("resize", this.screenAdapter);
    // 加载完成后自动适配分辨率
    this.screenAdapter();
  }, //生命周期-挂载完成（可以访问DOM元素）
  activated() {}, //如果页面有keep-alive缓存功能,这个函数会触发
  beforeUpdate() {}, //生命周期-更新之前
  updated() {}, //生命周期-更新之后
  beforeDestroy() {}, //生命周期-销毁之前
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    // 销毁回调函数
    this.$socket.unRegisterCallBack("trend");
  }, //生命周期-销毁完成
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 9;
  color: #fff;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-item {
    background-color: #222733;
  }
}
</style>